using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectLoaiCamNang()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAICAMNANG
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectLoaiCamNangPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAICAMNANG,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLoaiCamNang", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteLoaiCamNang(int id)
		{
			return Json(new{
				Success = _repLoaiCamNang.Delete(id),
				RecordCount = _repLoaiCamNang.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult EditLoaiCamNang(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAICAMNANG,
				CurrentPage = page,
				EditID = id
			};
			return PartialView("Templates/TH_EditLoaiCamNang", CreateViewModel(data));
		}

		public ActionResult CancelEditingLoaiCamNang(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAICAMNANG,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLoaiCamNang", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateLoaiCamNang(FormCollection forms)
		{
			string id = forms.GetValues("LoaiCamNang_ID")[0];
			string tenloaicamnang = forms.GetValues("LoaiCamNang_TenLoaiCamNang")[0];
			string currentpage = forms.GetValues("LoaiCamNang_CurrentPage")[0];
			var loaicamnang = _repLoaiCamNang.SelectByID(int.Parse(id));
			loaicamnang.TenLoaiCamNang = tenloaicamnang;
			var result = _repLoaiCamNang.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAICAMNANG,
				CurrentPage = int.Parse(currentpage)
			};
            return View("SelectLoaiCamNang", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertLoaiCamNang(FormCollection forms)
		{
			string tenloaicamnang = forms.GetValues("LoaiCamNang_TenLoaiCamNang")[0];
			var newItem = new LoaiCamNang
			{
				TenLoaiCamNang = tenloaicamnang
			};
			var result = _repLoaiCamNang.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAICAMNANG,
				Added = result
			};
            return View("SelectLoaiCamNang", CreateViewModel(data));
		}


	}
}